import 'dart:async';
import 'dart:isolate';

/// CreateTime 2020/6/5 18:00
/// CreateUser chen 
/// ProjectName flutter_elf
import 'package:flutter_elf/config/component_index.dart';
import 'package:flutter_elf/widgets/common_widgets.dart';

class IsolatePage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return IsolatePageState();
  }
}
class IsolatePageState extends State{

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    init();
  }
  ReceivePort rcvPort;
  init(){
    rcvPort = ReceivePort();
    rcvPort.listen(((msg){
      print("${DateTime.now().toIso8601String()}=====>" + msg);
    }));
  }

  static Timer timer;
  static send(SendPort sendPort){
    timer = Timer.periodic(Duration(seconds: 1), (se){
      sendPort.send('message$se');
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBarCreator.create(context, 'isolate'),
      body: Column(
        children: <Widget>[
          buildButton("send", (){
            Isolate.spawn(send, rcvPort.sendPort);
          })
        ],
      ),
    );
  }

  @override
  void dispose() {
    timer?.cancel();
    timer = null;
    rcvPort?.close();
    // TODO: implement dispose
    super.dispose();
  }


}